package com.alon.spring01.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 仗剑远游,游必有方
 *
 * @author yulongguang
 * @version 1.0.0
 * @date 2019/1/12 15:45
 * @Description 时间和Cron表达式互转工具类
 */
public class ViolationCronUtils {
    private static final String pattern = "ss mm HH dd MM ?" ;

    private static final SimpleDateFormat sdf = new SimpleDateFormat(pattern);

    /**
     * 将时间类型按照格式转成cron字符串;
     * @param date        指定时间;
     * @param dateFormat  格式;
     */
    public static String formatDateByPattern(Date date, String dateFormat){
        String formatTimeStr = null;
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }

    /**
     * 获取cron表达式;
     * @param date 指定时间;
     */
    public static String getCron(Date  date){
        return formatDateByPattern(date, pattern);
    }
}
